/**
 * SimpleKeywordFilter.java
 * @author Daniel Liu
 * Create: 2007-1-9 ����09:48:03
 * NOTE: 
 * 	�򻯵Ĺؼ��ֹ�����������ִ�Сд�����ҹؼ��ּ�Ҳ������ı�
 */
package org.lex.filter;

public class SimpleKeywordFilter implements Filter<CharSequence> {
	// ����洢��ʵ������תΪСд�ġ��ؼ��֡������в���ִ�Сд�ıȽ�
	protected String[] keywords = null;
	// ��̵Ĺؼ��ֵĳ���
	protected int keywordMinimumLength = Integer.MAX_VALUE;

	public SimpleKeywordFilter(CharSequence... keys) {
		super();
		if (null == keys)//
			throw new NullPointerException("Keywords couldn't be null");
		if (0 == keys.length)
			throw new IllegalArgumentException("No keyword found");
		this.keywords = new String[keys.length];
		for (int i = 0; i < keys.length; i++) {
			CharSequence key = keys[i];
			if (null == key)
				throw new NullPointerException("Null keyword in arguments");
			this.keywords[i] = key.toString().toLowerCase();
			if (this.keywords[i].length() < keywordMinimumLength)
				keywordMinimumLength = this.keywords[i].length();
		}
	}

	public boolean accept(CharSequence o) {
		// ����ʹ��keywordMinimumLength������жϿ��ܴ�������
		// ��Ϊ��˵��ĳЩ�����д�д��Сд���ַ�ĳ��ȿ��ܻ�ı�
		if (null == o || o.length() < this.keywordMinimumLength)
			return false;

		String source = o.toString().toLowerCase();
		for (String keyword : this.keywords) {
			if (keyword.length() > source.length())
				continue;
			if (true == source.contains(keyword))
				return true;
		}
		return false;
	}
}
